<?php
    /**
     * Created by JetBrains NetBeans.
     * User: yinyancai
     * Date: 13-3-14 下午4:44
     * To change this template use File | Settings | File Templates.
     */
    if (!defined('IN_MOBILE_API')) {
        exit('Access Denied');
    }
    include_once 'forum.php';
    class cloud_api {
        function common() { }

        function output() {

            //判断是否缺少参数
            if (empty($_GET['uid']) || empty($_GET['event'])) {
                $variable = array(
                    'code' => 1001,
                    'messagestr' => lang('plugin/cloud', '1001'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }

            $event = C::t('#cloud#cloud_coupon_rule')->fetch_all_event($_GET['event']);
            if(empty($event)){
                $variable = array(
                    'code' => 90006,
                    'messagestr' => lang('plugin/cloud', '90006'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }
            $tokens = array();
            foreach($event as $k=>$v){
                $rule = C::t('#cloud#cloud_coupon_rule')->fetch_info($v['id']);
                if(empty($rule)){
                    $variable = array(
                        'code' => 1002,
                        'messagestr' => lang('plugin/cloud', '1001'),
                    );
                    cloud_core::result(cloud_core::variable($variable));
                }
                //新增
                $data = array(
                    'uid' =>$_GET['uid'],
                    'rule_id' => $rule['id'],
                    'rule' => $rule['rule'],
                    'type' => $rule['type'],
                    'time' => $rule['time'],
                    'status' => 1,
                );

                $id = C::t('#cloud#cloud_coupon')->insert($data);
                //如果插入失败，这返回错误信息
                if (empty($id)) {
                    $variable = array(
                        'code' => 80002,
                        'messagestr' => lang('plugin/cloud', '80002'),
                    );
                    cloud_core::result(cloud_core::variable($variable));
                }
                $token = rand(10,99).time();
                $data_token = array('token'=>$token);
                C::t('#cloud#cloud_coupon')->update($id,$data_token);
                $tokens[$k] = ($token);

            }
            //正常返回
            $variable = array(
                'data' => array(
                    'token'=>$tokens,
                    'code' => 80001,
                    'messagestr' => lang('plugin/cloud', '80001')),

            );
            cloud_core::result(cloud_core::variable($variable));
        }
    }

?>

